package com.gph.etrip;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import com.gph.etrip.routehandler.*;

public class ActivityMap extends MapActivity {

	LinearLayout linearLayout;
	Road mRoad;
	MapView mapView;
	Context context;
	Rect rect;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);
		mapView = (MapView) findViewById(R.id.mapViewId);
		mapView.setBuiltInZoomControls(true);
		
		DisplayMetrics display = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(display);
		rect = new Rect(0, 0, display.widthPixels, display.heightPixels);
		
		// TODO: coordinates are hard coded, modify later
		new Thread() {
			@Override
			public void run() {
//				double fromLat = 57.70683289001359, fromLon = 11.937433889900149, toLat = 57.71453000978562, toLon = 12.038376143966616;
//				String url = RoadProvider.getUrl(fromLat, fromLon, toLat, toLon);
//				//url=http://maps.google.com/maps?f=d&hl=en&saddr=57.70683289001359,11.937433889900149&daddr=57.71453000978562,12.038376143966616&ie=UTF8&0&om=0&output=kml
//				String url = "http://maps.google.com/maps/ms?msid=218301669927716437366.0004b27a8c6de1530e4c9&msa=0&ll=57.706707,11.938921&spn=0.004557,0.016512&ie=UTF8&0&om=0&output=kml";
//				InputStream is = getConnection(url);
//				mRoad = RoadProvider.getRoute(is);
//				mHandler.sendEmptyMessage(0);
				//the following try-catch statement for testing
				InputStream is = null;
				try{
					is = getAssets().open("eTrip_Test_01_Lindholmen.kml");
//					is = getAssets().open("maps.kml");
					mRoad = RoadProvider.getRoute(is);
//					for(int i=0; i<mRoad.mPoints.length; i++){
//						System.out.println(mRoad.mPoints[i].mName +"%"+mRoad.mPoints[i].mDescription+"%"
//											+mRoad.mPoints[i].mIconUrl+"%"+mRoad.mPoints[i].mLatitude);
//					}
					mHandler.sendEmptyMessage(0);					
				}catch(FileNotFoundException e){
				      //System.out.println("File" + file.getAbsolutePath() + " could not be found on filesystem");
				    }
				catch(IOException e){
					System.out.println("File" + " could not be found on filesystem");
				}							
			}
		}.start();
	}

	Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			TextView textView = (TextView) findViewById(R.id.description);
			textView.setText(mRoad.mName + " " + mRoad.mDescription);
			MapOverlay mapOverlay = new MapOverlay(ActivityMap.this,mRoad, mapView,rect);
			List<Overlay> listOfOverlays = mapView.getOverlays();
			listOfOverlays.clear();
			listOfOverlays.add(mapOverlay);
			mapView.invalidate();
		};
	};

	private InputStream getConnection(String url) {
		InputStream is = null;
		try {
			URLConnection conn = new URL(url).openConnection();
			is = conn.getInputStream();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return is;
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}


